<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
*/
//error_reporting(2047);
class Production extends MY_Controller{

	public function __construct() {
		parent::__construct();
		$this->load->model('Article_model','article');
		$this->load->model('Production_model','product');
		$this->load->model('Order_model','order');
		$this->load->model('Category_model','c');
		$this->load->model('Tag_model','tag');
		$this->load->model('Tagrelation_model','tr');
		$this->load->library('form_validation');
		if ($this->isAdmin() == false) redirect("admin/login");
	}

	public function index()
	{
		$this->listProduction();
	}

	public function listProduction()
	{
		$sdata = array();
		$postdata = $this->input->post();
		$s_name='';
		if (!empty($postdata['s_name']))
		{
			$s_name = $postdata['s_name'];
			$sdata['name like \'%' .$s_name.'%\''] = '';
		}
		$this->_p['pagenumb'] = 20;
		$list_t = $this->product->L($sdata,'*', $this->_p['pagenumb'], ($this->_p['pagecur'] == 0) ? 0 : ($this->_p['pagecur']-1) * $this->_p['pagenumb'],'productid','asec');
		if (!empty($list_t))
		{
			foreach ($list_t as $k => $v) $cateIdAry[] = $v['cateid'];
			$cateId = implode(',',$cateIdAry);
			$categoryList_t = $this->c->L(array('cateid in (' . $cateId . ')' =>''));
			$categoryList = cate2array($categoryList_t, 'cateid');
			
			foreach ($list_t as $k => $v)
			{
				if (!empty($categoryList[$v['cateid']])) $list_t[$k]['cateinfo'] = $categoryList[$v['cateid']];
				
			}
		}
		$this->_p['pagecount'] = $this->input->post('pagecount');
		if (empty($this->_p['pagecount'])) 
		{
			$this->_p['pagecount'] = $this->product->C($sdata);
		}
		$this->_d['page'] = eyPage($this->_p,$sdata);
		$this->_d['pagecount'] = $this->_p['pagecount'];
		$this->_d['list'] = $list_t;
		$this->_d['s_name'] = $s_name;
		$this->load->view($this->_d['cfg']['tpl__admin'] . 'production/list', $this->_d);
	}

	public function add($func = 'production')
	{

		if ($this->form_validation->run('production/edit') == false)
		{
			if (validation_errors() != '')
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = validation_errors();
				exit(json_encode($retmsg));
			}

			$this->_d['action'] = 'add';
			$row = $this->product->INIT();
			$row['func'] = $func;
			$this->_d['row'] = $row;
			$catedata =  cate2list(0, $this->c->getCateData('production'));
			$this->_d['parentcate'] = array2option($catedata, '', 1);
			$this->load->view($this->_d['cfg']['tpl__admin'] . 'production/detail', $this->_d);
		}
		else
		{
			$postdata = $this->input->post();
			$postdata['ctime'] = time();
			if (!empty($postdata['homeimage'])) $postdata['imgthumb'] = $postdata['homeimage'];
			if (!empty($postdata['homesourceimage'])) $postdata['sourceimage'] = $postdata['homesourceimage'];
			$productionid = $this->product->A($postdata);
			if ($productionid > 0)
			{
				//附件入库
				if (!empty($postdata['imgsource']))
				{
					// 原图
					$source_imgthumb = $postdata['imgsource'];
					// 切割图
					$imgthumb = $postdata['attachpath'];

					// 附件入库
					$this->load->module("attached/upload/addAttach",array($productionid,'production',$imgthumb,$source_imgthumb));
				}

				// 关键字入库
				if (!empty($postdata['keywords']))
				{
					$keywords = str_replace('，',',',$postdata['keywords']);
					$keyarray = explode(',',$keywords);
				}
				else
				{
					$keyarray = array();
				}
				

				// 增加批量标签
				$this->tag->setTags($keyarray, array(),$productionid, 'production');
				// 更新标签统计
				$this->tag->resetTagNum();

				$admininfo = $this->isAdmin();
				$this->action_log('1',$admininfo['user_id'],"添加产品",$postdata['name']);
				$retmsg['code'] = '1';
				$retmsg['msg'] = $this->lang->line('comm_sucess_tip');
				exit(json_encode($retmsg));
			}
			else
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = $this->lang->line('comm_fail_tip');
				exit(json_encode($retmsg));
			}
		}
	}

	public function delProduction($productid)
	{
		$sdata['productid']=$productid;
		$adinfo = $this->product->O($sdata);
		$this->product->D($sdata);
		// 附件删除
		$this->load->module("attached/upload/delAttach",array($productid,'production'));
		// 标签删除
		$this->tag->delTag($productid,'production');
		$admininfo = $this->isAdmin();
		$this->action_log('1',$admininfo['user_id'],"删除产品",$adinfo['name']);
		$retmsg['code'] = '1';
		$retmsg['msg'] = $this->lang->line('success');
		exit(json_encode($retmsg));
	}

	public function delMoreProduction()
	{
		$postdata = $this->input->post('productid');
		if(!empty($postdata))
		{
			if(is_array($postdata))
			{
				$productid = implode(',',$postdata);
			}
			$adinfo = $this->product->L(array('productid in (' . $productid . ')' => ''));
			foreach ($adinfo as $k => $v)
			{
				$adname[] = $v['name'];
			}
			$this->product->D(array('productid in (' . $productid . ')' => ''));
			// 附件删除
			$this->load->module("attached/upload/delAttach",array($postdata,'production'));
			// 标签删除
			$this->tag->delTag($postdata,'production');
			$admininfo = $this->isAdmin();
			$this->action_log('1',$admininfo['user_id'],"批量删除产品",implode(',',$adname));
			$retmsg['code'] = '1';
			$retmsg['msg'] = $this->lang->line('success');
			exit(json_encode($retmsg));
		}
		else
		{
			$retmsg['code'] = '0';
			$retmsg['msg'] = $this->lang->line('fail');
			exit(json_encode($retmsg));
		}
	}

	public function editProduction($productid)
	{
		if ($this->form_validation->run('production/edit') == false)
		{
			if (validation_errors() != '')
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = validation_errors();
				exit(json_encode($retmsg));
			}
			$this->_d['action'] ="editProduction";
			$sdata['productid'] = $productid;
			$this->_d['row'] = $this->product->O($sdata);
			$catedata =  cate2list(0, $this->c->getCateData('production'));
			$this->_d['parentcate'] = array2option($catedata, $this->_d['row']['cateid'], 1);
			// 附件取得
			$attach = $this->getAttach($productid,'production');
			if ($attach)
			{
				$this->_d['attach'] = $attach;
			}
			$this->load->view($this->_d['cfg']['tpl__admin'] . 'production/detail', $this->_d);
		}
		else
		{
			$postdata = $this->input->post();
			$production = $this->product->O(array('productid'=>$productid));

			//如果有删除附件先删除
			$this->load->model('Attacheddetail_model', 'attach');

			if (!empty($postdata['delimg']))
			{
				foreach ($postdata['delimg'] as $k => $v)
				{
					if (!empty($v))
					{
						$attachdetail = $this->attach->O(array('attachid'=>$v));
						if ($production['imgthumb'] == $attachdetail['attachpath'])
						{
							$this->product->M(array('imgthumb'=>'','sourceimage'=>''),array('productid'=>$productid));
						}
						$this->attach->D(array('attachid'=>$v));
					}
				}
				
			}
			//附件入库
			if (!empty($postdata['imgsource']))
			{
				// 原图
				$source_imgthumb = $postdata['imgsource'];
				// 切割图
				$imgthumb = $postdata['attachpath'];

				// 附件入库
				$this->load->module("attached/upload/addAttach",array($productid,'production',$imgthumb,$source_imgthumb));
			}

			// 关键字入库
			if (!empty($postdata['keywords']))
			{
				$keywords = str_replace('，',',',$postdata['keywords']);
				$keyarray = explode(',',$keywords);
			}
			else
			{
				$keyarray = array();
			}
			
			$oldtr = $this->tr->L(array('relationid'=>$productid,'channel'=>'production'));
			if (!empty($oldtr))
			{
				foreach ($oldtr as $k => $v)
				{
					$oldtag = $this->tag->O(array('tagid'=> $v['tagid']));
					$oldtr[$k]['tagname'] = @$oldtag['tagname'];
				}
			}
			// 增加批量标签
			$this->tag->setTags($keyarray, $oldtr,$productid, 'production');
			// 更新标签统计
			$this->tag->resetTagNum();

			$sdata['productid'] = $productid;
			$postdata['mtime'] = time();
			if (!empty($postdata['homeimage'])) $postdata['imgthumb'] = $postdata['homeimage'];
			if (!empty($postdata['homesourceimage'])) $postdata['sourceimage'] = $postdata['homesourceimage'];
			if ($this->product->M($postdata,$sdata) >0)
			{
				$admininfo = $this->isAdmin();
				$this->action_log('1',$admininfo['user_id'],"编辑产品",$postdata['name']);
				$retmsg['code'] = '1';
				$retmsg['msg'] = $this->lang->line('comm_sucess_tip');
				exit(json_encode($retmsg));
			}
			else
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = $this->lang->line('comm_fail_tip');
				exit(json_encode($retmsg));
			}
		}
	}

	public function copyProduction($productid)
	{
		if (!empty($productid))
		{
			$row = $this->product->O(array('productid'=>$productid));
			unset($row['productid']);
			unset($row['keywords']);
			$row['status'] = 0;
			if ($this->product->A($row))
			{
				$admininfo = $this->isAdmin();
				$this->action_log('1',$admininfo['user_id'],"复制产品",$row['name']);
				$retmsg['code'] = '1';
				$retmsg['msg'] = '复制成功';
				exit(json_encode($retmsg));
			}
		}
		$retmsg['code'] = '0';
		$retmsg['msg'] = '产品参数丢失';
		exit(json_encode($retmsg));
	}

	public function sethomepage($id)
	{
		$postdata = $this->input->post();
		if ($this->product->M($postdata,array('productid'=>$id)))
		{
			$retmsg['code'] = '1';
			$retmsg['msg'] = '设置成功';
			exit(json_encode($retmsg));
		}
		else
		{
			$retmsg['code'] = '0';
			$retmsg['msg'] = '设置失败';
			exit(json_encode($retmsg));
		}
	}

	public function order()
	{
		if (!$this->admin_priv('order'))
		{
			show_error("您没有权限进行此操作！");
		}
		$sdata = array();
		$list = $this->order->L($sdata,'*',$this->_p['pagenumb'], ($this->_p['pagecur'] == 0) ? 0 : ($this->_p['pagecur']-1) * $this->_p['pagenumb']);
		$this->_p['pagecount'] = $this->input->post('pagecount','ctime','desc');
		if (empty($this->_p['pagecount'])) 
		{
			$this->_p['pagecount'] = $this->order->C($sdata);
		}
		$this->_d['page'] = eyPage($this->_p,$sdata);
		$this->_d['list'] = $list;
		$this->_d['pagecount'] = $this->_p['pagecount'];
		$this->load->view($this->_d['cfg']['tpl__admin'] . 'production/order', $this->_d);
	}

	public function reply($orderid)
	{
		if (!$this->admin_priv('order'))
		{
			show_error("您没有权限进行此操作！");
		}
		if ($this->form_validation->run('production/reply') == false)
		{
			if (validation_errors() != '')
			{
				$retmsg['code'] = '0';
				$retmsg['msg'] = validation_errors();
				exit(json_encode($retmsg));
			}
			$this->_d['row'] = $this->order->O(array('id'=>$orderid));
			$this->load->view($this->_d['cfg']['tpl__admin'] . 'production/order_detail', $this->_d);
		}
		else
		{
			$postdata = $this->input->post();
			
			if ($postdata['oldstatus'] != $postdata['is_reply'])
			{
				if ($postdata['is_reply'] == '1')
				{
					if (empty($this->_d['cfg']['smtp_server']))
					{
						$retmsg['code'] = '0';
						$retmsg['msg'] = '发邮件需要设置你的SMTP参数';
						exit(json_encode($retmsg));
					}
					if (empty($this->_d['cfg']['admin_email']))
					{
						$retmsg['code'] = '0';
						$retmsg['msg'] = '请设置你的管理员邮箱';
						exit(json_encode($retmsg));
					}
				}
			}
			if ($this->order->M($postdata,array('id'=>$orderid)))
			{
				if ($postdata['oldstatus'] != $postdata['is_reply'])
				{
					if ($postdata['is_reply'] == '1')
					{
						// 发送邮箱
						$this->load->library('email'); //加载CI的email类 
						$emailinfo=array('protocol' => 'smtp',
							'smtp_host' => $this->_d['cfg']['smtp_server'],
							'smtp_user' => $this->_d['cfg']['smtp_username'],
							'smtp_pass' => $this->_d['cfg']['smtp_passwd'],
							'smtp_port' => '25',
							'charset' => 'utf-8',
							'wordwrap' => TRUE,
							'mailtype' => 'html');

						$this->email->initialize($emailinfo);
						//以下设置Email内容  
						$this->email->from($emailinfo['smtp_user']);  
						$this->email->to($postdata['email']);  
						$this->email->subject($postdata['reply_title']);
						$content = $postdata['reply_content'];
						$this->email->message($content);  
						$this->email->send();
						$retmsg['code'] = '1';
						$retmsg['msg'] = '已发回复邮件给客户';
						exit(json_encode($retmsg));
					}
					else
					{
						$retmsg['code'] = '1';
						$retmsg['msg'] = '修改成功';
						exit(json_encode($retmsg));
					}
				}
				else
				{

					$retmsg['code'] = '1';
					$retmsg['msg'] = '修改成功';
					exit(json_encode($retmsg));
				}
			}
		}
		
	}

	public function delOrder($id)
	{
		if (!$this->admin_priv('order'))
		{
			show_error("您没有权限进行此操作！");
		}
		$sdata = array('id'=>$id);
		//$adinfo = $this->user->O($sdata);
		$this->order->D($sdata);
		$admininfo = $this->isAdmin();
		$this->action_log('1',$admininfo['user_id'],"删除询价");
		$retmsg['code'] = '1';
		$retmsg['msg'] = $this->lang->line('success');
		exit(json_encode($retmsg));
	}

	public function delMoreOrder()
	{
		if (!$this->admin_priv('order'))
		{
			show_error("您没有权限进行此操作！");
		}
		$postdata = $this->input->post('id');
		if(!empty($postdata))
		{
			if(is_array($postdata))
			{
			$orderid = implode(',',$postdata);
			}
			$this->order->D(array('id in(' . $orderid . ')'=>''));
			$admininfo = $this->isAdmin();
			$this->action_log('1',$admininfo['user_id'],"批量删除询价");
			$retmsg['code'] = '1';
			$retmsg['msg'] = $this->lang->line('success');
			exit(json_encode($retmsg));
		}
		else
		{
			$retmsg['code'] = '0';
			$retmsg['msg'] = $this->lang->line('fail');
			exit(json_encode($retmsg));
		}
	}

}

?>